package de.gameduell.framework.application.view {
	import de.gameduell.framework.application.Window;
	import de.gameduell.framework.application.controller.Controller;
	import de.gameduell.framework.application.event.GameEvent;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.gui.button.ToggleButton;
	import de.gameduell.framework.locale.Locale;
	import de.gameduell.framework.locale.LocaleData;
	import de.gameduell.framework.util.Audio;
	import de.gameduell.framework.util.Library;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;	

	/**
	 * @author sdur
	 */
	public class SimpleView {
		protected var baseController:Controller;
		
		protected var window:Window;
		protected var frame:Sprite;
		protected var content:Sprite;
		protected var localeData:LocaleData;
		protected var locale:Locale;
		protected var audio:Audio;	
		protected var closeButton:Button;
		protected var soundButton:ToggleButton;
		protected var musicButton:ToggleButton;
		protected var tipButton:ToggleButton;
		protected var library:Library;

		public function SimpleView(aWindow:Window, aController:Controller){
			window      = aWindow;
			frame		= window.getFrame();
			audio       = Audio.getInstance();
			baseController = aController;
			content     = window.getContent();
			locale      = Locale.getInstance();
			localeData  = LocaleData.getInstance();
			library		= Library.getInstance();
			
			initView();
		}
		
		protected function initView():void{
			var closeButtonSprite:Sprite = frame.getChildByName("closeButton") as Sprite;
			if(closeButtonSprite != null){
				closeButton = new Button(closeButtonSprite);
				closeButton.addEventListener(MouseEvent.CLICK, abortGame);
				closeButton.active  = true;
				closeButton.toolTip = true;
			}		
			var musicButtonSprite:Sprite = frame.getChildByName("musicButton") as Sprite;
			if(musicButtonSprite != null){
				musicButton = new ToggleButton(musicButtonSprite);
				musicButton.addEventListener(MouseEvent.CLICK, onMusicBtnClick);
				musicButton.active  = true;
				musicButton.toolTip = true;
			}
			var soundButtonSprite:Sprite = frame.getChildByName("soundButton") as Sprite;
			if(soundButtonSprite != null){
				soundButton = new ToggleButton(soundButtonSprite);
				soundButton.addEventListener(MouseEvent.CLICK, onSoundBtnClick);
				soundButton.active  = true;
				soundButton.toolTip = true;
			}
			var tipButtonSprite:Sprite = frame.getChildByName("tipButton") as Sprite;
			if(tipButtonSprite != null){
				tipButton =  new ToggleButton(tipButtonSprite);
				tipButton.addEventListener(MouseEvent.CLICK, onTipButtonClick);
				tipButton.active  = true;
				tipButton.toolTip = true;
				tipButton.toggled = baseController.tipsEnabled;
				
				baseController.addEventListener(GameEvent.TIPS_ENABLE_CHANGED, onTipsEnableChanged);
			}
		}
		
		protected function onTipsEnableChanged(event:GameEvent = null):void {
			if(tipButton.toggled !=  baseController.tipsEnabled){
				tipButton.toggled = baseController.tipsEnabled;
			}
		}

		protected function onTipButtonClick(event:MouseEvent):void {
			baseController.tipsEnabled = tipButton.toggled;
		}

		protected function onMusicBtnClick(event:MouseEvent):void {
			audio.toggleMuteAmbient();
		}
		
		protected function onSoundBtnClick(event:MouseEvent):void {
			audio.toggleMuteSound();
		}
		
		protected function abortGame(event:Event):void {
			baseController.sendAbort();
			baseController.exit();
		}
	}
}
